package org.spidermesh.meshlog.core.log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spidermesh.meshlog.config.MeshDict;
import org.spidermesh.meshlog.core.log.interfaces.LogManager;
import org.spidermesh.meshlog.core.persist.interfaces.Persist;
import org.spidermesh.meshlog.model.LogInfo;
import org.spidermesh.meshlog.properties.MeshLogProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @PackageName:org.spidermesh.meshlog.core.log.interfaces
 * @ClassName:DefaultLogOption
 * @Description:
 * @Author:hw
 * @Date:2020-12-14 14:04
 */
@Component
@ConditionalOnBean(value = Persist.class)
public class DefaultLogManager implements LogManager {
    @Autowired
    private Persist persist;
    private static final Logger logger= LoggerFactory.getLogger(DefaultLogManager.class);
    private static final DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public void doLog(LogInfo logInfo, MeshLogProperties properties) {
        MeshDict meshDict= MeshDict.valueOf("LOG_PERSIST_"+properties.getPersist().toUpperCase());
        switch (meshDict){
            case LOG_PERSIST_DB:
                logInfo.setPersistTime(LocalDateTime.now());
                persist.writeLog(logInfo);
                break;
            case LOG_PERSIST_NO:
                logger.info(String.format("\n[%s %s]-[%s] %s：%s",logInfo.getPrefix(),dtf.format(LocalDateTime.now()),logInfo.getTag(),logInfo.getLacation(),logInfo.getInfo()));
            default:
                break;
        }
    }

    @Override
    public void doLog(LogInfo logInfo, MeshDict logtype, Logger logger) {

    }
}
